import java.util.*;;
public class _12131 {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        int t= sc.nextInt();
        for(int i=0;i<t;i++){
            int n = sc.nextInt();
            System.out.println(fuck(n));
        }
    }
    public static int fuck(int n){
        int end=0;
        if(n<=9){
            return n;
        }else if(n>1000){
            end+=27;
        }
        else if(n>=100){
            end+=18;
        }else if(n>=10){
            end+=9;
        }
        if(end>20){
            end+=(n/1000);
        }else if(end>11){
            end+=(n/100);
        }
        else{
            end+=(n/10);
        }
        return end;
    }
}
